// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   Masking.java

package include.excel_export;

import java.io.File;
import java.util.*;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

// Referenced classes of package include.excel_export:
//			Path

public class Masking extends Path
{

	private SAXBuilder builder;
	private Document doc;
	private Element root;

	public Masking(String fileName)
	{
		builder = null;
		doc = null;
		root = null;
		init(fileName);
	}

	public Masking()
	{
		builder = null;
		doc = null;
		root = null;
	}

	public void setFile(String fileName)
	{
		init(fileName);
	}

	private void init(String fileName)
	{
		String file = (new StringBuilder(String.valueOf(getPath()))).append("/conf/").append(fileName).toString();
		try
		{
			builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
			doc = builder.build(new File(file));
			root = doc.getRootElement();
		}
		catch (Exception je)
		{
			je.printStackTrace();
		}
	}

	public Document getDocument()
	{
		return doc;
	}

	public Vector getTableNicks()
	{
		Vector result = new Vector();
		for (Iterator tables = getTables().iterator(); tables.hasNext(); result.addElement(getTableNick((Element)tables.next())));
		return result;
	}

	public Vector getTableNames()
	{
		Vector result = new Vector();
		for (Iterator tables = getTables().iterator(); tables.hasNext(); result.addElement(getTableName((Element)tables.next())));
		return result;
	}

	public HashMap getTableNameAndNicks()
	{
		HashMap result = new HashMap();
		Element table;
		for (Iterator tables = getTables().iterator(); tables.hasNext(); result.put(getTableName(table), getTableNick(table)))
			table = (Element)tables.next();

		return result;
	}

	public HashMap getColumnNameAndNicks(String tablenick)
	{
		if (getTable(tablenick) != null)
		{
			HashMap result = new HashMap();
			Element col;
			for (Iterator loop = getColumns(getTable(tablenick)).iterator(); loop.hasNext(); result.put(getName(col), getNick(col)))
				col = (Element)loop.next();

			return result;
		} else
		{
			return null;
		}
	}

	public Vector getColumnNames(String tablenick)
	{
		if (getTable(tablenick) != null)
		{
			Vector result = new Vector();
			Element col;
			for (Iterator loop = getColumns(getTable(tablenick)).iterator(); loop.hasNext(); result.addElement(getName(col)))
				col = (Element)loop.next();

			return result;
		} else
		{
			return null;
		}
	}

	public Vector getColumnNicks(String tablenick)
	{
		if (getTable(tablenick) != null)
		{
			Vector result = new Vector();
			Element col;
			for (Iterator loop = getColumns(getTable(tablenick)).iterator(); loop.hasNext(); result.addElement(getNick(col)))
				col = (Element)loop.next();

			return result;
		} else
		{
			return null;
		}
	}

	public Vector getColumnUsedTags(String tablenick)
	{
		if (getTable(tablenick) != null)
		{
			Vector result = new Vector();
			Element col;
			for (Iterator loop = getColumns(getTable(tablenick)).iterator(); loop.hasNext(); result.addElement(getUsedTag(col)))
				col = (Element)loop.next();

			return result;
		} else
		{
			return null;
		}
	}

	public Vector getColumnRequireds(String tablenick)
	{
		if (getTable(tablenick) != null)
		{
			Vector result = new Vector();
			Element col;
			for (Iterator loop = getColumns(getTable(tablenick)).iterator(); loop.hasNext(); result.addElement(getRequired(col)))
				col = (Element)loop.next();

			return result;
		} else
		{
			return null;
		}
	}

	public Vector getColumnAttributes(String tablenick, String attribute)
	{
		if (getTable(tablenick) != null)
		{
			Vector result = new Vector();
			Element col;
			for (Iterator loop = getColumns(getTable(tablenick)).iterator(); loop.hasNext(); result.addElement(getAttribute(col, attribute)))
				col = (Element)loop.next();

			return result;
		} else
		{
			return null;
		}
	}

	public String getColumnName(String tablenick, String columnnick)
	{
		Element table = getTable(tablenick);
		for (Iterator loop = getColumns(table).iterator(); loop.hasNext();)
		{
			Element col = (Element)loop.next();
			if (getNick(col).equals(columnnick))
				return getName(col);
		}

		return null;
	}

	public String getTableNameFormNick(String tablenick)
	{
		return getTableName(getTable(tablenick));
	}

	public String getTableNameFromNick(String tablenick)
	{
		return getTableName(getTable(tablenick));
	}

	public String getTableNickFromName(String table)
	{
		HashMap map = getTableNameAndNicks();
		if (map == null)
			return null;
		else
			return (String)map.get(table);
	}

	public Element getTable(String tablenick)
	{
		for (Iterator loop = getTables().iterator(); loop.hasNext();)
		{
			Element table = (Element)loop.next();
			if (getTableNick(table).equals(tablenick))
				return table;
		}

		return null;
	}

	private List getTables()
	{
		List list = root.getChild("tables").getChildren("table");
		return list;
	}

	private String getTableNick(Element table)
	{
		return table.getAttributeValue("nick");
	}

	private String getTableName(Element table)
	{
		return table.getAttributeValue("name");
	}

	private List getColumns(Element table)
	{
		List list = table.getChild("columns").getChildren("column");
		return list;
	}

	private int getColumnsSize(Element table)
	{
		return getColumns(table).size();
	}

	private String getNick(Element column)
	{
		return column.getAttributeValue("nick");
	}

	private String getName(Element column)
	{
		return column.getAttributeValue("name");
	}

	private String getUsedTag(Element column)
	{
		return column.getAttributeValue("usedTag");
	}

	private String getRequired(Element column)
	{
		return column.getAttributeValue("required");
	}

	private String getAttribute(Element column, String attribute)
	{
		return column.getAttributeValue(attribute);
	}
}
